package com.spark.colliery.module.mine.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.spark.colliery.R
import com.spark.common.BaseFragment
import com.spark.common.ext.alert
import com.spark.common.widget.ConfirmAlert
import kotlinx.android.synthetic.main.fragment_profile_change_password.*

/**
 * change the password
 */
class ProfileChangePasswordFragment : BaseFragment() {

    companion object {

        fun newInstance(): ProfileChangePasswordFragment {

            val args = Bundle()

            val fragment = ProfileChangePasswordFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.fragment_profile_change_password
    }

    private lateinit var confirmAlert: ConfirmAlert

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tv_title.text = "修改密码"
        iv_back.setOnClickListener { pop() }

        btn_submit.setOnClickListener { checkBeforSubmit() }
        confirmAlert = ConfirmAlert(context)
    }


    private fun checkBeforSubmit() {
        val currentPassword = et_current_password!!.text.toString().trim { it <= ' ' }
        val newPassword = et_new_password!!.text.toString().trim { it <= ' ' }
        val confirmPassword = et_confirm_password!!.text.toString().trim { it <= ' ' }

        if (TextUtils.isEmpty(currentPassword)) {
            confirmAlert.show("请输入当前密码")
            return
        }

        //todo 检验用户密码。。。

        if (TextUtils.isEmpty(newPassword)) {
            confirmAlert.show("请输入新密码")
            return
        }

        if (TextUtils.isEmpty(confirmPassword)) {
            confirmAlert.show("请再次确认新密码")
            return
        }

        if (newPassword != confirmPassword) {
            confirmAlert.show("新密码两次输入不正确")
            return
        }
        alert("修改成功")
        pop()
    }
}
